package jdbc_study.dbcp;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.dbcp2.BasicDataSource;
/**
 * @author ChenYongHao
 * @date 2023/5/8 11:16
 * @desc 数据库连接池
 */

public class DBCPTest {

    public static void main(String[] args) throws SQLException {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/reggie");
        dataSource.setUsername("root");
        dataSource.setPassword("123456");

        Connection connection = dataSource.getConnection();
        String sql = "select * from admin";
        PreparedStatement prepareStatement = connection.prepareStatement(sql);
        ResultSet resultSet = prepareStatement.executeQuery();
        while (resultSet.next()) {
            Object id = resultSet.getObject(1);
            Object username = resultSet.getObject(2);
            Object password = resultSet.getObject(3);
            System.out.println(id + ":" + username + ":" + password);
        }
        resultSet.close();
        connection.close();
        dataSource.close();
    }

}

